package com.hissy.common;

import java.util.List;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;

/**
 * Created by zhangyanfei on 2018/9/5.
 * 静态文件加载地址
 *
 * @author zhangyanfei
 * @date 2018/09/05
 */
public class MultiDomUrl implements TemplateMethodModelEx {
    private static final int FIVE = 5;
    /**
     * 静态资源 域名
     */
    private String host = null;
    /**
     * 静态资源 tag
     */
    private String hostTag = null;
    /**
     * 静态资源 number
     */
    private String hostNumber = null;
    /**
     * 静态资源起始编号
     */
    private int hostStartNum = 0;

    @Value("${com.hissy.cn.web.imgHost}")
    private String imgHost;

    private String minSuffix = null;

    @Override
    public Object exec(List list) throws TemplateModelException {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        //获取资源文件名
        SimpleScalar simpleScalar = (SimpleScalar)list.get(0);

        String url = simpleScalar.getAsString();

        if (!imgHost.endsWith("/")) {
            imgHost = imgHost + "/";
        }

        if (url.startsWith("/")) {
            url = url.substring(1);
        }
        return imgHost + url;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getHostTag() {
        return hostTag;
    }

    public void setHostTag(String hostTag) {
        this.hostTag = hostTag;
    }

    public String getHostNumber() {
        return hostNumber;
    }

    public void setHostNumber(String hostNumber) {
        this.hostNumber = hostNumber;
    }

    public int getHostStartNum() {
        return hostStartNum;
    }

    public void setHostStartNum(int hostStartNum) {
        this.hostStartNum = hostStartNum;
    }

    public String getMinSuffix() {
        return minSuffix;
    }

    public void setMinSuffix(String minSuffix) {
        this.minSuffix = minSuffix;
    }
}
